package com.data.center.service.impl;

import com.data.center.dto.OrganizationDto;
import com.data.center.entity.OrganizationProfile;
import com.data.center.entity.OrganizationTag;
import com.data.center.entity.OrganizationProfile;
import com.data.center.exception.exception.BizException;
import com.data.center.mapper.OrganizationMapper;
import com.data.center.mapper.OrganizationProfileMapper;
import com.data.center.mapper.OrganizationTagMapper;
import com.data.center.mapper.OrganizationProfileMapper;
import com.data.center.service.OrganizationProfileService;
import com.data.center.util.ForeignManage.ForeignDataUtil;
import com.data.center.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Li_Jx
 * @description:
 * @date 2023/12/5 15:00
 */
@Service
public class OrganizationProfileServiceImpl implements OrganizationProfileService {
    @Autowired
    private OrganizationProfileMapper organizationProfileMapper;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private OrganizationTagMapper organizationTagMapper;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private ForeignDataUtil foreignDataUtil;




    @Override
    public void update(OrganizationProfile organizationProfile){
        String id = organizationProfile.getId();
        String organizationId = organizationProfile.getOrganizationId();
        String tagId = organizationProfile.getTagId();
        if(id == null || id.isEmpty()){
            throw new BizException("标签id为空");
        }
        OrganizationProfile byId = organizationProfileMapper.findById(id);
        if (byId == null){
            throw new BizException("id无对应数据");
        }
        if (organizationId != null){
            OrganizationDto organizationDto = organizationMapper.selectOrganizationById(organizationId);
            if (organizationDto == null){
                throw new BizException("组织id无对应数据");
            }
        }
        if (tagId != null){
            List<OrganizationTag> organizationTagList = organizationTagMapper.findById(tagId);
            if (organizationTagList.size() == 0){
                throw new BizException("标签id无对应数据");
            }
        }
        //查查有没有重复数据
        OrganizationProfile testObject = new OrganizationProfile();
        testObject.setOrganizationId(organizationId);
        testObject.setTagId(tagId);
        List<OrganizationProfile> select = organizationProfileMapper.select(testObject);
        if (select.size() != 0){
            throw new BizException("此数据已存在");
        }
        organizationProfileMapper.update(organizationProfile);
    }

    @Override
    public void insert(OrganizationProfile organizationProfile) {
        String organizationId = organizationProfile.getOrganizationId();
        String tagId = organizationProfile.getTagId();
        if (organizationId != null){
            OrganizationDto organizationDto = organizationMapper.selectOrganizationById(organizationId);
            if (organizationDto == null){
                throw new BizException("组织id无对应数据");
            }
        }
        if (tagId != null){
            List<OrganizationTag> organizationTagList = organizationTagMapper.findById(tagId);
            if (organizationTagList.size() == 0){
                throw new BizException("标签id无对应数据");
            }
        }

        //查查有没有重复数据
        OrganizationProfile testObject = new OrganizationProfile();
        testObject.setOrganizationId(organizationId);
        testObject.setTagId(tagId);
        List<OrganizationProfile> select = organizationProfileMapper.select(testObject);
        if (select.size() != 0){
            throw new BizException("此数据已存在");
        }
        //插入数据
        organizationProfile.setId(String.valueOf(snowflakeIdWorker.nextId()));
        organizationProfile.setCreator("");
        organizationProfile.setCreationTime(new Date());
        organizationProfileMapper.insert(organizationProfile);
    }

    @Override
    public List<OrganizationProfile> searchByForeignKey(String id) {
        OrganizationProfile organizationFilter = new OrganizationProfile();
        organizationFilter.setOrganizationId(id);
        OrganizationProfile organizationTagFilter = new OrganizationProfile();
        organizationTagFilter.setTagId(id);
        List<OrganizationProfile> selectOrganization = organizationProfileMapper.select(organizationFilter);
        List<OrganizationProfile> selectTag = organizationProfileMapper.select(organizationTagFilter);
        ArrayList<OrganizationProfile> OrganizationProfiles = new ArrayList<>();
        OrganizationProfiles.addAll(selectOrganization);
        OrganizationProfiles.addAll(selectTag);
        Set<OrganizationProfile> set = new HashSet<>(OrganizationProfiles);
        List<OrganizationProfile> resultList = new ArrayList<>(set);
        return resultList;

    }

    @Override
    public int deleteByForeignKey(String id) {
        List<OrganizationProfile> organizationProfileList = searchByForeignKey(id);
        if (organizationProfileList.size() == 0){
            return 0;
        }
        for (OrganizationProfile item : organizationProfileList) {
            organizationProfileMapper.deleteById(item.getId());
        }
        return 0;
    }

    @Override
    public int mergeByForeignKey(String id, String deletedId) {
        if (id.equals(deletedId)){
            return  0;
        }
        OrganizationProfile organizationProfile = new OrganizationProfile();
        OrganizationProfile organizationFilter = new OrganizationProfile();
        organizationFilter.setOrganizationId(deletedId);
        OrganizationProfile organizationTagFilter = new OrganizationProfile();
        organizationTagFilter.setTagId(deletedId);
        List<OrganizationProfile> selectOrganization = organizationProfileMapper.select(organizationFilter);
        List<OrganizationProfile> selectTag = organizationProfileMapper.select(organizationTagFilter);
        if (selectOrganization.size() > 0) {
            for (OrganizationProfile item : selectOrganization) {
                organizationProfile.setOrganizationId(id);
                organizationProfile.setTagId(item.getTagId());
                if (organizationProfileMapper.select(organizationProfile).size() > 0){
                    throw new BizException("此标签已存在，无法融合");
                }
                item.setOrganizationId(id);
                update(item);
            }
        }
        if (selectTag.size() > 0) {
            for (OrganizationProfile item : selectTag) {
                organizationProfile.setOrganizationId(item.getOrganizationId());
                organizationProfile.setTagId(id);
                if (organizationProfileMapper.select(organizationProfile).size() > 0){
                    throw new BizException("此标签已存在，无法融合");
                }
                item.setTagId(id);
                update(item);
            }
        }
        return 0;
    }

}
